package com.example.demo01;

import com.fasterxml.jackson.databind.ser.std.StdKeySerializers.Default;
import java.util.*;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/9/9 10:57
 * @version:v1.0
 */
public class TestRadissonTemple {

    private RedisTemplate redisTemplate;

    public void test() {

    }

    //1.删除单个key
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    //2.删除多个key
    public void deleteKeys(String... keys) {
        redisTemplate.delete(keys);
    }

    //3.指定失效时间
    public void expire(String key, long time) {
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    //4.获得失效时间
    public long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    //5.判断key是否存在
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    //1.添加String缓存
    public void stringTemple(String key, String value, Long time, TimeUnit timeUnit) {
        //1.设置值
        redisTemplate.boundValueOps(key).set(value);
        redisTemplate.boundValueOps(key).set(value, time, timeUnit);

        //2.设置值String
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.opsForValue().set(key, value, time, timeUnit);

        //3.过期时间
        redisTemplate.expire(key, time, timeUnit);

        //4.获得值
        String getValue = (String) redisTemplate.boundValueOps(key).get();
        String getValue1 = (String) redisTemplate.opsForValue().get(key);

        //5.删除key
        redisTemplate.delete(key);

        //6.顺序递增
        redisTemplate.boundValueOps(key).increment(3L);

        //7.顺序递减
        redisTemplate.boundValueOps(key).increment(-3L);
    }

    //2.Hash
    public void mapTemple(String key, Map map, Long time, TimeUnit timeUnit) {
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);
        HashOperations hashOperations = redisTemplate.opsForHash();

        //1. 添加一个键值对
        boundHashOperations.put(key,map.get(key));
        redisTemplate.boundHashOps(key).put(key,map.get(key));
        hashOperations.put(key,key,map.get(key));

        //2. 添加一个map集合
        boundHashOperations.putAll(map);
        redisTemplate.boundHashOps(key).putAll(map);

        //3.获得所有小key
        Set key3 = boundHashOperations.keys();
        Set key1 = redisTemplate.boundHashOps(key).keys();
        Set key2 = hashOperations.keys(key);

        //4.提取所有的value
        List value3 = boundHashOperations.values();
        List value1 = redisTemplate.boundHashOps(key).values();
        List value2 = hashOperations.values(key);

        //5.获得所有的键值对集合
        Map entries3 = boundHashOperations.entries();
        Map entries1 = redisTemplate.boundHashOps(key).entries();
        Map entries2 = hashOperations.entries(key);

        //6.删除小key
        boundHashOperations.delete(key);

        //7.判断hash中是否有该值
        Boolean isEmpty = redisTemplate.boundHashOps(key).hasKey(key);

    }

    //3.set
    public void setTemple(String key, String smallKey, Set set){
        BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key);

        //1.设置
        boundSetOperations.add(key);
        boundSetOperations.add(set);

        //3.获得值
        Set set1 = boundSetOperations.members();

        //4.判断是否存在
        Boolean isEmpty = boundSetOperations.isMember(smallKey);

        //5.删除一个小key
        Long remove = boundSetOperations.remove(smallKey);

        //6.获得set缓存的长度
        Long size = boundSetOperations.size();

    }

    // 4.list
    public void listTemple(String key, List list,String listValue){
        BoundListOperations boundListOperations = redisTemplate.boundListOps(key);

        boundListOperations.leftPush(listValue);
        boundListOperations.rightPush(listValue);

        boundListOperations.rightPushAll(list);
        boundListOperations.leftPushAll(list);

        List range = boundListOperations.range(0, 10);

        String o =(String)boundListOperations.leftPop();
        String o1 =(String)boundListOperations.rightPop();

        String index = (String)boundListOperations.index(1);
    }

    //5.Zset
    public void zSetTemple(String key,Map<String,Double> map, String smallKey){
        BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps(key);
        //1.插入数据并设置分数
        boundZSetOperations.add(smallKey,map.get(smallKey));

        //2.插入多个元素
        DefaultTypedTuple<String> p1 = new DefaultTypedTuple<>(smallKey,map.get(smallKey));
        DefaultTypedTuple<String> p2 = new DefaultTypedTuple<>(smallKey,map.get(smallKey));
        boundZSetOperations.add(new HashSet<>(Arrays.asList(p1,p2)));




    }


}
